﻿using System;
using System.Data;
using System.Reflection;
using l1cs.server.model;
using l1cs.server.model.Instance;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class LightSpawnTable
    {
        private static Logger _log = Logger.getLogger( typeof( LightSpawnTable ).FullName );

        private static LightSpawnTable _instance;

        public static LightSpawnTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new LightSpawnTable();
            }
            return _instance;
        }

        private LightSpawnTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM spawnlist_light";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable(sqlstr).Rows )
                {
                    L1Npc l1npc = NpcTable.getInstance().getTemplate( SqlUtils.toInt( item["npcid"]) );
                    if ( l1npc != null && l1npc.getImpl() == "L1FieldObject" )
                    {
                        String s = l1npc.getImpl();
                        L1FieldObjectInstance field = new L1FieldObjectInstance( l1npc );
                        field.setId( IdFactory.getInstance().nextId() );
                        field.setX( SqlUtils.toInt( item["locx"] ) );
                        field.setY( SqlUtils.toInt( item["locy"] ) );
                        field.setMap( SqlUtils.toShort( item["mapid"] ) );
                        field.setHomeX( field.getX() );
                        field.setHomeY( field.getY() );
                        field.setHeading( 0 );
                        field.setLightSize( l1npc.getLightSize() );

                        L1World.getInstance().storeObject( field );
                        L1World.getInstance().addVisibleObject( field );
                    }
                }
            }
        }
    }
}